<?php
namespace Kxdr\Controller;
use Think\Controller;
class MedalController extends ExtendController {
		private $HTM = "KxdrHonoraryMedal";
		protected $index_template = "index";
		protected $curgetway;
		public function index(){
			Vendor('Page');
			$tp = I("tp");
			$curgetway = I("gw")?I("gw"):$this->curgetway;
			
			$TOOL = D("ToolView");
			$tcfg = current($TOOL->where("`status`.`sid`=".$this->sid." AND `templet_code`='kxdr_honor'")->getField("status,version,cfg,tool_id"));
			if(!$tcfg){
				$this->success("您尚未开通插件：我的互动-荣誉，请先开通插件。",U("Shopmanage/chajian/index"));
				die();
			}
			if($tcfg["cfg"]){
				$tcfg["cfg"] = json_decode($tcfg["cfg"]);
			}
			
			$where = $tp?" AND `status`=1":" AND `status`=0";
			$where .= $curgetway?" AND `getway`='".$curgetway."'":"";
			$HT = M($this->HTM);
			$total = $HT->where("`sid`=".$this->sid.$where)->count("`id`");
			$Page = new \Page($total,18);
			$show = $Page->show();
			
			$honors = $HT->where("`sid`=".$this->sid.$where)->order("`getway`,`score`")->limit($Page->firstRow.','.$Page->listRows)->getField("`id`,`getway`,`score`,`title`,`pic_url`");
			$METAL_GETWAY = C("METAL_GETWAY");
			foreach($honors as $k=>$h){
				$honors[$k]["getway_set"]=$METAL_GETWAY[$h["getway"]];
			}
			$this->assign("tp",$tp);
			$this->assign("curgetway",$curgetway);
			$this->assign("curgetway_set",$METAL_GETWAY[$curgetway]);
			$this->assign("honors",$honors);
			$this->assign("getway",$METAL_GETWAY);
			$this->assign("tcfg",$tcfg);
			$this->assign("show",$show);
			$this->display($this->index_template);
		}
		public function offical_preview(){
			$this->HTM="KxdrHonoraryMedalDefault";
			$this->index_template="offical_preview";
			$this->index();
		}
		public function user(){			
			Vendor('Page');
			$usernick = I("usernick");
			$medal_id = I("medal_id");
			$curgetway = I("gw")?I("gw"):$this->curgetway;
			$USR = D("Kxdr/MedalUsersView");
			
			$where["muser.sid"] = $this->sid;
			if($curgetway){
				$where["getway"] = $curgetway;
			}
			if($medal_id){
				$where["muser.medal_id"] = $medal_id;
			}
			if($usernick){
				$where["user.usernick"] = $usernick;
			}
			
			$total = $USR->where($where)->count();
			$Page = new \Page($total,30);
			$show = $Page->show();
			
			$users = $USR->where($where)->limit($Page->firstRow.','.$Page->listRows)->getField("medal_user_id,getway,score,title,pic_url,medal_num,get_time,raise_time,mixednick,usernick");
			$METAL_GETWAY = C("METAL_GETWAY");
			foreach($users as $k=>$usr){
				$users[$k]["getway"] = $METAL_GETWAY[$usr["getway"]]["name"];
			}
			$HT = M($this->HTM);
			$hwh["sid"]=$this->sid;
			if($curgetway){
				$hwh["getway"]=$curgetway;
			}
			$honors = $HT->where($hwh)->order("`getway`,`score`")->getField("`id`,`title`");
			
			$this->assign("show",$show);
			$this->assign("honors",$honors);
			$this->assign("users",$users);
			$this->assign("medal_id",$medal_id);
			$this->assign("getway",$METAL_GETWAY);
			$this->assign("curgetway",$curgetway);
			$this->assign("usernick",$usernick);
			$this->display();
		}
		public function delete_target_honor(){
			$id = I("id");
			$HT = M("KxdrHonoraryMedal");
			$status["status"]=1;
			$re = $HT->where("`sid`=".$this->sid." AND `id`=".$id)->save($status);
			if(!$re){
				$data["hasError"]=true;
				$data["error"]="修改失败！".$HT->getDbError();
			}else{
				$data["hasError"]=false;
			}
			$this->ajaxReturn($data);
		}
		public function edit_target_honor(){
			$data["id"]=I("id");
			$data["score"]=I("target_score");
			$data["title"]=I("target_title");
			$data["getway"]=I("getway");
			$data["pic_url"]=I("pic_url");
			$HT = M("KxdrHonoraryMedal");
			$re = $HT->save($data);
			if(!$re){
				$data["hasError"]=true;
				$data["error"]="修改失败！".$HT->getDbError();
			}
			$this->ajaxReturn($data);
		}
		public function renew_target_honor(){
			$data["id"]=I("id");
			$data["sid"]=$this->sid;
			$data["status"]=0;
			$HT = M("KxdrHonoraryMedal");
			$re = $HT->save($data);
			if(!$re){
				$data["hasError"]=true;
				$data["error"]="修改失败！".$HT->getDbError();
			}
			$this->ajaxReturn($data);
		}
		public function add_target_honor(){
			$data["sid"]=$this->sid;
			$data["score"]=I("target_score");
			$data["title"]=I("target_title");
			$data["getway"]=I("getway");
			$data["pic_url"]=I("pic_url");
			$HT = M("KxdrHonoraryMedal");
			$data["id"]=$HT->add($data);
			if($data["id"]){
				$data["hasError"]=false;
			}
			$this->ajaxReturn($data);
		}
		public function add_honor_title(){
			$data["honor_title"]["action"] = I("action");
			$data["honor_title"]["exact"] = I("exact");
			$data["honor_title"]["fault"] = I("fault");
			$json = json_encode($data);
			$TOOL = D("ToolView");
			$re = $TOOL->where("`status`.`sid`=".$this->sid." AND `templet_code`='kxdr_honor'")->save(Array("cfg"=>$json));
			$reda["hasError"] = !$re;
			$reda["error"] = $TOOL->getDbError();
			$this->ajaxReturn($reda);
		}
		public function load_default_medal(){
			$gw = I("getway");
			$where = $gw?" WHERE `getway`='".$gw."'":"";
			$Model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
			$re = $Model->execute("INSERT INTO `sm_kxdr_honorary_medal` (`getway`, `score`, `sid`, `title`, `pic_url`, `cfg`, `status`) SELECT `getway`, `score`, ".$this->sid.", `title`, `pic_url`, `cfg`, `status`  FROM `sm_kxdr_honorary_medal_default` $where");
			if(!$re){
				$data["hasError"]=true;
				$data["error"]=$Model->getDbError();
			}else{
				$data["hasError"]=false;
			}
			$this->ajaxReturn($data);
		}
		public function empty_medal(){
			$curgetway = I("curgetway");
			$where = $curgetway?" AND `getway`='".$curgetway."'":"";
			$HT = M("KxdrHonoraryMedal");
			$re = $HT->where("`sid`=".$this->sid.$where)->save(Array("status"=>1));
			$this->ajaxReturn($re);
		}
		public function unempty_medal(){
			$curgetway = I("curgetway");
			$where = $curgetway?" AND `getway`='".$curgetway."'":"";
			$HT = M("KxdrHonoraryMedal");
			$re = $HT->where("`sid`=".$this->sid.$where)->save(Array("status"=>0));
			$this->ajaxReturn($re);
		}
		public function get_medal_as_gift(){
			$tp = I("tp");
			$where = $tp?" AND `getway`<>'as_a_gift'":" AND `getway`='as_a_gift'";
			if($tp=="all"||$tp=="rule_"){
				$where="";
			}
			$HT = M("KxdrHonoraryMedal");
			$re = $HT->where("`sid`=".$this->sid.$where)->order("`getway`")->getField("`id`,`title`, `pic_url`",true,true);
			if(!$re){
				$data["hasError"]=true;
				if(!$tp){
					$data["error"]="  您还没有设置作为奖品发放的奖章，请先进入<a href=\"/?m=Kxdr&c=Medal\" target=\"_blank\" >设置</a>";
				}elseif($tp=="all"||$tp=="rule_"){
					$data["error"]="  您还没有奖章，请先进入<a href=\"/?m=Kxdr&c=Medal\" target=\"_blank\" >设置</a>";
				}else{
					$data["error"]="  您还没有设置基本奖章，请先进入<a href=\"/?m=Kxdr&c=Medal\" target=\"_blank\" >设置</a>";
				}
			}else{
				$data["data"]=array_values($re);
				$data["hasError"]=false;
			}
			// $data = json_decode(json_encode($data),true);
			$this->ajaxReturn($data);
		}
}